package zuoye5;

import java.util.*;



class StudentMgar {

    //根据学号删除学生信息
    public static void delById(int id){
        Set<Student > set = new HashSet<>() ;
        for (Object obj:set){
           Student stu=(Student)obj;
           if (stu.getId()==id){
            set.remove(stu);}
        }
    }

    //根据学号修改学生信息
        public static void UpdaById(int id,String name){
            Set<Student > set = new HashSet<>() ;
        for (Object obj:set ){
            Student stu=(Student)obj;
            if (stu.getId()==id){
                int id2=stu.getId();
                int age2=stu.getAge();
                String tell2=stu.getTell();
                set.remove(stu);
                Student student=new Student (id2,name,age2,tell2);
                set.add(student);
            }

        }
    }

    public static Set searchByname(String name){
        Set<Student > set = new HashSet<>() ;
        Set tempSet=new HashSet();
        for(Student student:set){
            if (student.getName().indexOf(name)!=-1){
                tempSet.add(student);
            }
        }
        return tempSet;
    }

    public static void main(String[] args) {
        Set<Student > set = new HashSet<>() ;
        Student scott = new Student(1000, "scott", 21, "23221");
        Student tom = new Student(1001, "tom", 22, "153543");
        Student alice = new Student(1002, "alice", 23, "18234");
        Student james = new Student(1003, "james", 24, "87755");
        Student jack = new Student(1004, "jack", 25, "54521");
        set.add(scott);
        set.add(tom);
        set.add(alice);
        set.add(james);
        set.add(jack);
        Scanner input = new Scanner(System.in);
        boolean flag = true;

            System.out.println("===学生信息管理系统===");
            System.out.println("1.增加学生信息");
            System.out.println("2.根据学号删除信息");
            System.out.println("3.根据学号修改信息");
            System.out.println("4.根据姓名模糊查找");
            System.out.println("0.退出");
            System.out.println("=====================");
            System.out.println("请选择:");
            int choice = input.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("请输入学号:");
                    int id = input.nextInt();
                    System.out.println("请输入姓名:");
                    String name = input.next();
                    System.out.println("请输入年龄:");
                    int age = input.nextInt();
                    System.out.println("请输入号码:");
                    String sex = input.next();
                    Student student = new Student(id, name, age, sex);
                     set.add(student);
                     for (Object obj:set){
                         Student stu=(Student)obj;
                         System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge()+","+stu.getTell());
                     }

                case 2:
                    System.out.println("请输入你想删除的学生的学号");
                    int id1=input.nextInt();
                    StudentMgar.delById(id1);
                    for (Object obj:set){
                        Student stu=(Student)obj;
                        System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge()+","+stu.getTell());
                    }
                case 3:
                    System.out.println("请输入你想修改的学生学号");
                    int id3=input.nextInt();
                    System.out.println("你要改成的名字：");
                    String name1=input.next();
                    StudentMgar.UpdaById(id3,name1);

                    for (Object obj:set){
                        Student stu=(Student)obj;
                        System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge()+","+stu.getTell());
                    }
                case 4:
                    System.out.println("请输入你要模糊查询的学生姓名");
                    String name4=input.next();
                    StudentMgar.searchByname(name4);
                    for (Object obj:set){
                        Student stu=(Student)obj;
                        System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge()+","+stu.getTell());
                    }

                case 0:
                    System.out.println("退出系统");
                default:
                    System.out.println("你输入的序号无效，请重新输入");
                    flag = true;
                    break;
            }

    }
    }




